package com.logic.modular.game.player.info;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.comment.pojo.domain.logic.domain.PlayerRank;
import com.logic.comment.util.RedisUtils;
import com.logic.comment.util.XSThreadUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 玩家段位信息(PlayerRank)表服务实现类
 *
 * @author cxy
 * @since 2024-09-09 18:30:36
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class PlayerRankServiceImpl extends PlayerRankBaseServiceImpl {

    @Resource
    private RedisUtils redisUtils;

    @Override
    public boolean updateById(PlayerRank entity) {
        boolean b = super.updateById(entity);
        saveCache(entity);
        return b;
    }

    public void init(Long playerId) {
        PlayerRank build = PlayerRank.builder()
                .uid(playerId)
                .build();
        build.insert();
        saveCache(build);
    }

    public void obtainExpReward(Long playerId, Long restValue) {
        PlayerRank playerRank = getById(playerId);
        playerRank.gainExperience(restValue);
        updateById(playerRank);
    }

    public void obtainIntegralReward(Long playerId, long resValue) {
        PlayerRank playerRank = getById(playerId);
        playerRank.gainRankIntegral(resValue);
    }

    public Set<PlayerRank> rank(Long playerId) {
        String jasonStr = (String) redisUtils.get(String.format(PLAYER_TANK_KEY, 1));
        List<PlayerRank> list = JSONUtil.toList(jasonStr, PlayerRank.class);
        Set<PlayerRank> playerRankList = new HashSet<>(list);
        PlayerRank one = getById(playerId);
        playerRankList.add(one);
        return playerRankList;
    }

    public Set<PlayerRank> playerGrade(Long playerId) {
        String jasonStr = (String) redisUtils.get(String.format(PLAYER_TANK_KEY, 2));
        List<PlayerRank> list = JSONUtil.toList(jasonStr, PlayerRank.class);
        Set<PlayerRank> playerRankList = new HashSet<>(list);
        PlayerRank one = getById(playerId);
        playerRankList.add(one);
        return playerRankList;
    }

    public Set<PlayerRank> winningPoints(Long playerId) {
        String jasonStr = (String) redisUtils.get(String.format(PLAYER_TANK_KEY, 3));
        List<PlayerRank> list = JSONUtil.toList(jasonStr, PlayerRank.class);
        Set<PlayerRank> playerRankList = new HashSet<>(list);
        PlayerRank one = getById(playerId);
        playerRankList.add(one);
        return playerRankList;
    }


    @Resource
    private XSThreadUtil xsThreadUtil;
    private static final String PLAYER_TANK_KEY = "player-rank:%d";

    /**
     * 每小时
     */
    @Scheduled(fixedDelay = 1000 * 60 * 60)
    public void listDayShop() {
        LambdaQueryWrapper<PlayerRank> wrapper1 = new LambdaQueryWrapper<PlayerRank>().orderByAsc(PlayerRank::getRank);
        LambdaQueryWrapper<PlayerRank> wrapper2 = new LambdaQueryWrapper<PlayerRank>().orderByAsc(PlayerRank::getGrade);
        LambdaQueryWrapper<PlayerRank> wrapper3 = new LambdaQueryWrapper<PlayerRank>().orderByAsc(PlayerRank::getRankIntegral);
        xsThreadUtil.executorService().submit(() -> createRanking(wrapper1, 1));
        xsThreadUtil.executorService().submit(() -> createRanking(wrapper2, 2));
        xsThreadUtil.executorService().submit(() -> createRanking(wrapper3, 3));
    }

    private void createRanking(LambdaQueryWrapper<PlayerRank> wrapper, int type) {
        List<PlayerRank> playerRanks = getPlayerRanks(wrapper);
        String jsonStr = JSONUtil.toJsonPrettyStr(playerRanks);
        redisUtils.set(String.format(PLAYER_TANK_KEY, type), jsonStr);
    }

    public List<PlayerRank> getPlayerRanks(LambdaQueryWrapper<PlayerRank> wrapper) {
        // 创建分页对象，指定每页大小为 100
        Page<PlayerRank> page = new Page<>(1, 100);
        // 执行分页查询
        Page<PlayerRank> resultPage = page(page, wrapper);
        // 获取查询结果
        List<PlayerRank> playerRanks = resultPage.getRecords();
        // 处理查询结果为空的情况
        if (playerRanks.isEmpty()) {
            return Collections.emptyList();
        }
        return playerRanks;
    }

}
